﻿using Microsoft.Practices.Unity;
using Model;
using Service;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Strawberry.Controllers
{
    public partial class HomeController : Controller
    {

        [Dependency]
        public IContactUsManager ContactUsManager { get; set; }
        [Dependency]
        public IArticleImageManager ArticleImageManager { get; set; }
        [Dependency]
        public IJobOfferManager JobManager { get; set; }
        [Dependency]
        public IArticleManager ArticleManager { get; set; }
        [Dependency]
        public IImageManager ImageManager { get; set; }

        public virtual ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public virtual ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public virtual ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public virtual ActionResult ContactUs()
        {

            return View();
        }

        [HttpPost]
        public virtual ActionResult ContactUs(ContactUs contactUs)
        {
            ContactUsManager.Add(contactUs);
            return RedirectToAction(MVC.Home.Index());
        }

        public virtual ActionResult ArticleImage()
        {
            return View();
        }

        [HttpPost]
        public virtual ActionResult ArticleImage(ArticleImage ArticleImage)
        {
            ArticleImageManager.Add(ArticleImage);
            return RedirectToAction(MVC.Home.Index());
        }

        public virtual ActionResult JobOffers()
        {
            var jobs = JobManager.Query();
            return View(jobs);
        }

        public virtual ActionResult JobOffer(int id)
        {
            var job = JobManager.Get(id);
            return View(job);
        }

        public virtual ActionResult Articles()
        {
            var list = ArticleManager.Query();
            return View(list);
        }


        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public virtual JsonResult SaveDynamicPicture()
        {
            if (Request.InputStream != null)
            {
                // IE A essayé if (String.IsNullOrEmpty(Request["qqfile"])) {
                string name = Request["qqfile"];

                string url = @"C:\temp\" + name;
                using (Bitmap b = new Bitmap(Request.InputStream))
                {
                    b.Save(url);
                }

                Model.Image image = new Model.Image
                {
                    Description = name,
                    Url = url,
                };
                ImageManager.Add(image);
            }
            return Json(new { success = true }, "application/json");
        }

        public virtual ActionResult ArticlesInDetail(int Id)
        {
            var monArticle = ArticleManager.Get(Id);
            return View(monArticle);
        }
    }
}
